package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.constant.Constants;
import com.zzyl.common.core.redis.RedisCache;
import com.zzyl.nursing.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.domain.NursingLevel;
import com.zzyl.nursing.service.INursingLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理等级Service业务层处理
 * 
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService
{
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询护理等级
     * 
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id)
    {
        NursingLevel nursingLevel = nursingLevelMapper.selectById(id);
        try {
            nursingLevel.setSearchValue(redisCache.getCacheMapValue(Constants.CACHE_NURSING_LEVEL_KEY, id.toString()));
        } catch (Exception e) {
            log.error("查询护理等级后缓存失败");
        }
        return nursingLevel;
    }

    /**
     * 查询护理等级列表
     * 
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel)
    {
        List<NursingLevel> nursingLevels = nursingLevelMapper.selectNursingLevelList(nursingLevel);
        try{
           redisCache.setCacheObject("nursingLevels",nursingLevels);
        } catch (Exception e) {
            log.error("查询护理等级列表后缓存失败");
        }
        return nursingLevels;
    }

    /**
     * 新增护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel)
    {
        int i = nursingLevelMapper.insert(nursingLevel);
        try {
            redisCache.deleteObject(Constants.CACHE_NURSING_LEVEL_KEY);
        } catch (Exception e) {
            log.error("新增护理等级后删除缓存失败");
        }
        return i;
    }

    /**
     * 修改护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel)
    {
        int i = nursingLevelMapper.updateById(nursingLevel);
        try {
            redisCache.deleteObject(Constants.CACHE_NURSING_LEVEL_KEY);
        } catch (Exception e) {
            log.error("修改护理等级后删除缓存失败");
        }
        return i;
    }

    /**
     * 批量删除护理等级
     * 
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids)
    {
        int i = nursingLevelMapper.deleteBatchIds(Arrays.asList(ids));
        try {
            redisCache.deleteObject(Constants.CACHE_NURSING_LEVEL_KEY);
        } catch (Exception e) {
            log.error("删除护理等级缓存失败");
        }
        return i;
    }

    /**
     * 删除护理等级信息
     * 
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id)
    {

        int i = nursingLevelMapper.deleteById(id);
        try {
            redisCache.deleteObject(Constants.CACHE_NURSING_LEVEL_KEY);
        } catch (Exception e) {
            log.error("删除护理等级缓存失败");
        }
        return i;
    }

    /**
     * 查询护理等级Vo列表
     *
     * @param nursingLevel 条件
     * @return 结果
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelVoList(NursingLevel nursingLevel) {
        List<NursingLevelVo> nursingLevelVos = nursingLevelMapper.selectNursingLevelVoList(nursingLevel);
        try {
            redisCache.setCacheObject("nursingLevelVos", nursingLevelVos);
        } catch (Exception e) {
            log.error("护理等级列表数据缓存失败");
        }
        return nursingLevelVos;
    }

    /**
     * 查询所有护理等级
     *
     * @return 列表
     */
    @Override
    public List<NursingLevel> listAll() {
        List<NursingLevel> list = null;
        try {
            // 从缓存获取数据
            list = redisCache.getCacheObject(Constants.CACHE_NURSING_LEVEL_KEY);
            // 缓存不为空则直接返回
            if (CollUtil.isNotEmpty(list)) {
                return list;
            }
        } catch (Exception e) {
            log.error("从缓存中获取护理等级列表数据失败", e);
        }
        // 缓存为空或获取失败，查询数据库
        LambdaQueryWrapper<NursingLevel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NursingLevel::getStatus, 1);
        list = list(queryWrapper);
        // 数据库查询结果存入缓存（即使结果为空，也可以缓存空集合避免重复查询）
        try {
            redisCache.setCacheObject(Constants.CACHE_NURSING_LEVEL_KEY, list);
        } catch (Exception e) {
            log.error("护理等级列表数据缓存失败", e);
        }
        return list;
    }
}
